package com.tupu.service.impl.dispatch;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tupu.entity.dispatch.SignalOmissionHistoryData;
import com.tupu.mapper.dispatch.SignalOmissionHistoryDataMapper;
import com.tupu.service.dispatch.SignalOmissionHistoryDataService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.util.*;
import java.util.stream.Collectors;


@Service
public class SignalOmissionHistoryDataServiceImpl extends ServiceImpl<SignalOmissionHistoryDataMapper, SignalOmissionHistoryData> implements SignalOmissionHistoryDataService {

    @Autowired(required = false)
    private SignalOmissionHistoryDataMapper signalOmissionHistoryDataMapper;

    @Override
    public Page<SignalOmissionHistoryData> historyDataPage(String dataType, String station, String problemType, Long page, Long size) {
        QueryWrapper<SignalOmissionHistoryData> queryWrapper = new QueryWrapper<>();
        Page<SignalOmissionHistoryData> info = new Page<>(page, size);
        if (StringUtils.isNotEmpty(dataType)) {
            queryWrapper.eq("device_type", dataType);
        }
        if (StringUtils.isNotEmpty(station)) {
            queryWrapper.eq("station", station);
        }
        if (StringUtils.isNotEmpty(problemType)) {
            queryWrapper.eq("problem_type", problemType);
        }
        signalOmissionHistoryDataMapper.selectPage(info, queryWrapper);
        return info;
    }

    @Override
    public List<String> findAllDataType() {
        List<String> list = new ArrayList<>();
        QueryWrapper<SignalOmissionHistoryData> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("data_type");
        List<SignalOmissionHistoryData> problemLists = signalOmissionHistoryDataMapper.selectList(queryWrapper);
        for (SignalOmissionHistoryData info : problemLists) {
            if (null != info){
                list.add(info.getDataType());
            }
        }
        List<String> stringList = list.stream().distinct().collect(Collectors.toList());
        stringList.remove("");
        return stringList;
    }

    @Override
    public List<String> findAllStation() {
        List<String> list = new ArrayList<>();
        QueryWrapper<SignalOmissionHistoryData> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("station");
        List<SignalOmissionHistoryData> problemLists = signalOmissionHistoryDataMapper.selectList(queryWrapper);
        for (SignalOmissionHistoryData info : problemLists) {
            if (null != info){
                list.add(info.getStation());
            }
        }
        List<String> stringList = list.stream().distinct().collect(Collectors.toList());
        stringList.remove("");
        return stringList;
    }

    @Override
    public List<String> findAllProblemType() {
        List<String> list = new ArrayList<>();
        QueryWrapper<SignalOmissionHistoryData> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("problem_type");
        List<SignalOmissionHistoryData> problemLists = signalOmissionHistoryDataMapper.selectList(queryWrapper);
        for (SignalOmissionHistoryData info : problemLists) {
            if (null != info){
                list.add(info.getProblemType());
            }
        }
        List<String> stringList = list.stream().distinct().collect(Collectors.toList());
        stringList.remove("");
        return stringList;
    }

    @Override
    public Map<String, Object> dataTypePercentage() {
        List<String> list = new ArrayList<>();
        QueryWrapper<SignalOmissionHistoryData> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("data_type");
        List<SignalOmissionHistoryData> problemLists = signalOmissionHistoryDataMapper.selectList(queryWrapper);
        for (SignalOmissionHistoryData info : problemLists) {
            if (null != info){
                list.add(info.getDataType());
            }
        }
        Map<String, Object> result = new HashMap<>();
        double size = list.size() * 1.0;
        Map<String, Long> map = list.stream().collect(Collectors.groupingBy(k -> k, Collectors.counting()));
        Set<String> strings = map.keySet();
        for (String s : strings) {
            DecimalFormat decimalFormat = new DecimalFormat("##.00%");
            String format = decimalFormat.format(map.get(s) / size);
            result.put(s, format);
        }
        return result;
    }

    @Override
    public Map<String, Object> stationPercentage() {
        List<String> list = new ArrayList<>();
        QueryWrapper<SignalOmissionHistoryData> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("station");
        List<SignalOmissionHistoryData> problemLists = signalOmissionHistoryDataMapper.selectList(queryWrapper);
        for (SignalOmissionHistoryData info : problemLists) {
            if (null != info){
                list.add(info.getStation());
            }
        }
        Map<String, Object> result = new HashMap<>();
        double size = list.size() * 1.0;
        Map<String, Long> map = list.stream().collect(Collectors.groupingBy(k -> k, Collectors.counting()));
        Set<String> strings = map.keySet();
        for (String s : strings) {
            DecimalFormat decimalFormat = new DecimalFormat("##.00%");
            String format = decimalFormat.format(map.get(s) / size);
            result.put(s, format);
        }
        return result;
    }


    @Override
    public Map<String, Object> problemTypePercentage() {
        List<String> list = new ArrayList<>();
        QueryWrapper<SignalOmissionHistoryData> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("problem_type");
        List<SignalOmissionHistoryData> problemLists = signalOmissionHistoryDataMapper.selectList(queryWrapper);
        for (SignalOmissionHistoryData info : problemLists) {
            if (null != info){
                list.add(info.getProblemType());
            }
        }
        Map<String, Object> result = new HashMap<>();
        double size = list.size() * 1.0;
        Map<String, Long> map = list.stream().collect(Collectors.groupingBy(k -> k, Collectors.counting()));
        Set<String> strings = map.keySet();
        for (String s : strings) {
            DecimalFormat decimalFormat = new DecimalFormat("##.00%");
            String format = decimalFormat.format(map.get(s) / size);
            result.put(s, format);
        }
        return result;
    }
}